//
//  TestViewController.m
//  09-静态单元格
//
//  Created by Sizhu Ba on 2017/9/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "TestViewController.h"
#import "CTMLObj.h"
#import "MLNodelObj.h"
#import "LJMMultiLevelTableView.h"
@interface TestViewController ()
@property (nonatomic, strong) LJMMultiLevelTableView * levelList;
@property (nonatomic, strong) NSArray * listobj;
@property (nonatomic, strong) UILabel *rule;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *  path = [[NSBundle mainBundle]pathForResource:@"territory" ofType:@"txt"];
    NSURL * url = [NSURL fileURLWithPath:path];
    NSData * data = [NSData dataWithContentsOfURL:url];
    NSError * error ;
    NSDictionary *dict =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    //NSLog(@"111111%@",dict);
    
    
   self.listobj =   [self getdata:dict];
    
   self.levelList = [[LJMMultiLevelTableView alloc]init:CGRectMake(0, 50, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height-200) andNeedPreservation:true andObjs:self.listobj];
    [self.listobj count];
    
    NSLog(@"111111%ld", [self.listobj count]);
    
    CTMLObj  * arr =self.listobj[0];
    NSLog(@"222222%ld",[arr.subNodes count]);
    [self.view addSubview:self.levelList];
    
    self.rule = [[UILabel alloc]initWithFrame:CGRectMake(80, 560, 375, 100)];
    //self.rule.text = @"消费金额除以10";
    //[self.rule sizeToFit];
    self.levelList.rule = self.rule;
    [self.view addSubview:self.rule ];
}
-(NSArray *)getdata:(NSDictionary *)dict {
    NSArray * array = dict[@"data"];
    
    //NSLog(@"222222%@",array);
    NSMutableArray * objs = [NSMutableArray array];
    for (NSDictionary * continent in array) {
        //  let continentName = continent["洲"] as! String
        NSString * continentName =  continent[@"洲"];
        //let continentid   = continent["continentID"] as! String
        NSString * continentid =   continent[@"continentID"];
        
        // let continentC = CTMLObj.node(ownID: continentid, name: continentName, level: 1)
        CTMLObj * continentC = [CTMLObj getCTMLObj:continentid andName:continentName andLevel:1];
        // objs.append(continentC)
        continentC.subNodes = [NSMutableArray<MLNodelObj*> array];
        [objs addObject:continentC];
        
        
        NSArray * countryArray = continent[@"国家"];
        if (countryArray != nil){
            for (NSDictionary * country in countryArray) {
                // let countryName = country["国家名称"] as! String
                NSString * countryName =  country[@"国家名称"];
                //  let countryid = country["countryID"]  as! String
                
                NSString * countryid =  country[@"countryID"];
                // let countryM = MLNodelObj.node(parentID: continentid, ownID: countryid, name: countryName, level: 2, isleaf: false, isroot: false, isExpand: false)
                
                MLNodelObj *  countryM =  [MLNodelObj sharedMLNodelObj:continentid andOwnID:countryid andName:countryName andLevel:2 andExpand:false andLeaf:true andRoot:false];
                
                [continentC.subNodes addObject:countryM];
                
                NSArray * cityArray = country[@"城市"];
                if (cityArray != nil){
                    for (NSDictionary * city in cityArray) {
                       // let cityName = city["城市"] as! String
                        NSString * cityName =  city[@"城市"];
                        //let cityid   = city["cityID"] as! String
                        NSString * cityid =  city[@"cityID"];
                        MLNodelObj *  cityM =  [MLNodelObj sharedMLNodelObj:countryid andOwnID:cityid andName:cityName andLevel:3 andExpand:true andLeaf:true andRoot:false];
                        //continentC.subNodes.append(cityM)
                        [continentC.subNodes addObject:cityM];
                    }
                }
                  NSLog(@"44444444%ld",[continentC.subNodes count]);
                
            }
        }
    }
    CTMLObj  * arr = objs[0];
  
    return objs;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
